<template>
    <div style="border: 1px solid #ccc;" v-if="isShow">
        <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
        />
        <Editor
            style="height: 350px; overflow-y: hidden;"
            v-model="html"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
        />
    </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

export default ({
    components: { Editor, Toolbar },
    data() {
        return {
            editor: null,
            html: '',
            toolbarConfig: { },
            editorConfig: { 
				placeholder: '请输入内容...',
				MENU_CONF:{
					uploadImage:{
						server:'',
						fieldName:'file',
						headers:{}
					},
					
				},
			},
            mode: 'default', // or 'simple'
			isShow:false
        }
    },
    methods: {
        onCreated(editor) {
            this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
        },
    },
    mounted() {
		let _this = this
		this.editorConfig.MENU_CONF.uploadImage = {
			customUpload(File,insertFn){
				
				var formdata = new FormData();
				formdata.append("file",File);
				
				let token = sessionStorage.getItem('token')
				let cid = sessionStorage.getItem('cid')
				formdata.append("cid",cid);
				let action = _this.$domain + '/uplodeFile/uplodeImageLocal'
				// if(_this.$env=='dev'){
				// 	action = _this.$domain + '/uplodeFile/uplodeImageLocal'
				// }else{
				// 	action = _this.$domain + '/uplodeFile/uplodeImageH5'
				// }
				_this.axios.post(action,formdata,{
						 headers: {
						   Authorization:token+'_'+cid
						 }
					 }).then(res => {
				   let result = res.data.data.img_url
				     insertFn(result)
				});
			}
		}
		
		this.isShow = true
    },
    beforeDestroy() {
        const editor = this.editor
        if (editor == null) return
        editor.destroy() // 组件销毁时，及时销毁编辑器
    }
})
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>